package org.example.flink;

import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.example.flink.pojo.WaterSensor;

import java.util.Arrays;

/**
 * @Author: tang
 * @Description:
 * @Date 2025/2/16 16:55
 */
public class FlinkRichFunction {

    public static void main(String[] args) throws Exception{
        StreamExecutionEnvironment environment = StreamExecutionEnvironment.getExecutionEnvironment();
        environment.fromCollection(
                Arrays.asList(
                        new WaterSensor("1", "11", 10),
                        new WaterSensor("1", "10", 11),
                        new WaterSensor("2", "22", 20),
                        new WaterSensor("3", "33", 30)
                )
        )
                .map(new RichMapFunction<WaterSensor,String>(){

                    @Override
                    public void open(Configuration parameters) throws Exception {
                        System.out.println("索引是：" + getRuntimeContext().getIndexOfThisSubtask() + " 的任务的生命周期开始");
                    }

                    @Override
                    public void close() throws Exception {
                        System.out.println("索引是：" + getRuntimeContext().getIndexOfThisSubtask() + " 的任务的生命周期结束");
                    }

                    @Override
                    public String map(WaterSensor value) throws Exception {
                        return value.getId();
                    }
                })
                .print()
        ;

        environment.execute();

    }

}
